package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.assistant.enums.eAssistantEscortCartState;
import com.yanqu.road.entity.beautyescort.EscortBeautyData;
import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortBeautyEvent;
import com.yanqu.road.entity.beautyescort.EscortDartTypeInfo;
import com.yanqu.road.entity.config.zhaoshang.ZsProjectInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogBeautyEscorting;
import com.yanqu.road.entity.log.LogZsProjectOpen;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.assistant.handler.UnionCrossActionHandler;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsConfigMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.stream.Collectors;

@Cmd(code = GameProtocol.S_ASSISTANT_ZS_ACTION_FROM_CROSS, desc = "小玉招商返回")
public class AssistantZSActionFromCross extends AbstractCommand {

    private static Logger logger = LogManager.getLogger(AssistantZSActionFromCross.class.getName());

    private AssistantProto.AssistantCrossActionRespMsg.Builder builder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        builder = AssistantProto.AssistantCrossActionRespMsg.parseFrom(packet.getBytes()).toBuilder();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 处理押镖响应
        AssistantProto.AssistantActionReqMsg.Builder assistantReq = builder.getAssistantActionReqMsg().toBuilder();
        AssistantProto.AssistantActionRespMsg.Builder assistantResp = builder.getAssistantActionRespMsg().toBuilder();

        Property preCost = PropertyHelper.parseStringToProperty(builder.getPreCost());
        //还原预扣记录
        player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(preCost));

        for (ZhaoShangProto.ZsProjectOpenRespMsg zsRespMsg : assistantResp.getZsProjectOpenRespMsgList()) {
            if (zsRespMsg.getRet() == 0) {
                //扣除道具
                ZsProjectInfo zsProjectInfo = ZsConfigMgr.getZsProjectInfoMap().get(zsRespMsg.getTemplateId());
                if (zsProjectInfo == null) {
                    continue;
                }
                Property consume = PropertyHelper.parseStringToProperty(zsProjectInfo.getConsume());
                consume.goodCountMultiNum(zsRespMsg.getNum());

                if (!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ZhaoShang, eLogMoneyType.ZsProjectAutoOpenConsume)) {
                    log.error("小玉招商 扣除道具失败 userId:{} zsId:{} consume:{}", player.getUserId(), zsRespMsg.getProjectId(), consume);
                }

                int decorationClass = GoodsMgr.getUserDecorationClass(player.getUserInfo().getDecoration());
                //立项日志
                AutoLogMgr.add(new LogZsProjectOpen(player.getUserId(), zsRespMsg.getProjectId(), decorationClass, player.getTitleId(), zsRespMsg.getTemplateId()));
                player.notifyListener(eGamePlayerEventType.ZhaoshangCanyu.getValue(),1);
            }
        }

        // 商会操作
        new UnionCrossActionHandler().handle(player, assistantReq, assistantResp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}

